package com.ujs.edu.medicine.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.ujs.edu.medicine.dao.PictureMSMapper;
import com.ujs.edu.medicine.dao.TableMSMapper;
import com.ujs.edu.medicine.dao.YunjiayiMedicineMapper;
import com.ujs.edu.medicine.dataobject.PictureMS;
import com.ujs.edu.medicine.dataobject.TableMS;
import com.ujs.edu.medicine.dataobject.YunjiayiMedicine;
import com.ujs.edu.medicine.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author:
 * @Date: Created in 22:51 2020/8/24
 * Description: 药品控制类
 * Modified By:
 */
@RestController
public class MedicineController {

    @Autowired
    YunjiayiMedicineMapper yunjiayiMedicineMapper;
    @Autowired
    TableMSMapper tableMSMapper;
    @Autowired
    PictureMSMapper pictureMSMapper;
    @Autowired
    private HttpServletRequest request;

    private String gettoken() {
        return request.getHeader("Authorization");
    }

    /*查询所有药品*/
    @RequestMapping("/api/shop/medicineList")
    public Result medicineList(){
        Result result=new Result();

        List<YunjiayiMedicine> medicines=yunjiayiMedicineMapper.selectAll();

        if (medicines!=null){
            result.setData(medicines);
            result.setCode(0);
            result.setMessage("查询成功");
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }
        return result;
    }

    /*查询商店里面的药品*/
    @RequestMapping("/api/shop/medicineListInShop")
    public Result getMedicine(@RequestParam("shopID") Integer id,@RequestParam("kind") Integer kind){
        Result result=new Result();

        List<TableMS> tableMS=tableMSMapper.getMedicinesInShop(id);

        if (tableMS==null){
            result.setCode(1);
            result.setMessage("没有药品");
            return result;
        }

        ArrayList<YunjiayiMedicine>medicines=new ArrayList<>();
        for (TableMS tableMS1:tableMS){
            List<String> pictureMS=pictureMSMapper.selectByMedicineId(tableMS1.getMedicineid());
            if(tableMS1.getMedicine().getKind()==kind||kind==1){
                tableMS1.getMedicine().setMainimg(pictureMS.get(0));
                medicines.add(tableMS1.getMedicine());
            }

        }

        if (medicines!=null){
            result.setCode(0);
            result.setData(medicines);
            result.setMessage("查询成功");
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }

    /*药品信息*/
    @RequestMapping("/api/shop/medicineInfo")
    public Result medicineInfo(@RequestParam("medicineID")Integer medicineID){
        Result result=new Result();

        YunjiayiMedicine medicine=yunjiayiMedicineMapper.selectByPrimaryKey(medicineID);

        if(medicine!=null){
            medicine.setPicture(pictureMSMapper.selectByMedicineId(medicineID));
            result.setData(medicine);
            result.setCode(0);
            result.setMessage("查询成功");
        }else {
            result.setCode(1);
            result.setMessage("查询失败");
        }

        return result;
    }

    /*添加药品*/
    @RequestMapping("/api/shopManage/addMedicine")
    public Result addMedicine(@RequestBody JSONObject object){
        Result result=new Result();

        String token=gettoken();
        int shopId = 0;
        try {
            shopId = JWT.decode(token).getClaim("id").asInt();
        } catch (Exception e) {
            result.setCode(1);
            result.setMessage("您还没有登陆，请登录");
            return result;
        }


        YunjiayiMedicine medicine1=yunjiayiMedicineMapper.selectByName(object.getString("name"));
        if (medicine1!=null){
           result.setCode(1);
           result.setMessage("已经有这个药品");
           return result;
        }

        YunjiayiMedicine medicine=new YunjiayiMedicine();
        TableMS tableMS=new TableMS();

        medicine.setMename(object.getString("name"));
        medicine.setPrescription(object.getInteger("identity"));
        medicine.setKind(Integer.parseInt(object.getString("kind")));
        medicine.setRepositoryNum(object.getInteger("repository_num"));
        medicine.setDescription(object.getString("see"));
        medicine.setPrice(object.getFloat("price"));



        int medic=yunjiayiMedicineMapper.insert(medicine);

        tableMS.setShopid(shopId);
        tableMS.setMedicineid(medicine.getMedicineId());

        tableMSMapper.insert(tableMS);

        PictureMS pictureMS=new PictureMS();
        JSONArray files=object.getJSONArray("picture");
        if (files!=null){
            for (Object file:files){
                System.out.println(file.toString());
                pictureMS.setPictureurl(file.toString());
                pictureMS.setMedicineid(medicine.getMedicineId());

                int mpic=pictureMSMapper.insert(pictureMS);
                if (mpic==0){
                    result.setCode(1);
                    result.setMessage("插入图片失败");
                    return result;
                }
            }
        }else {
            result.setCode(1);
            result.setMessage("没有图片，请插入图片");
            return result;
        }


        if (medic==1){
            result.setCode(0);
            result.setMessage("插入成功");
            result.setData(medicine.getMedicineId());
        }else {
            result.setCode(1);
            result.setMessage("插入失败");
        }

        return result;
    }

    /*删除药品*/
    @RequestMapping("/api/shopManage/deleteMedicine")
    public Result deleteMedicine(@RequestBody Map<String,Integer> map){
        Result result=new Result();

        YunjiayiMedicine medicine=yunjiayiMedicineMapper.selectByPrimaryKey(map.get("ID"));
        if (medicine==null){
            result.setCode(1);
            result.setMessage("数据库没有这个药品");
            return result;
        }

        int del=yunjiayiMedicineMapper.deleteByPrimaryKey(map.get("ID"));

        if (del==1){
            result.setCode(0);
            result.setMessage("删除成功");
        }else {
            result.setCode(1);
            result.setMessage("删除失败");
        }
        return result;
    }

    /*修改药品*/
    @RequestMapping("/api/shopManage/alterMedicine")
    public Result alterMedicine(@RequestBody JSONObject object){
        Result result=new Result();
        YunjiayiMedicine medicine1=yunjiayiMedicineMapper.selectByPrimaryKey(object.getInteger("id"));
        if (medicine1==null){
            result.setCode(1);
            result.setMessage("没有这个药品");
            return result;
        }

        medicine1.setMename(object.getString("name"));
        medicine1.setPrice(object.getFloat("price"));
        medicine1.setDescription(object.getString("see"));
        medicine1.setPrescription(object.getInteger("identity"));
        medicine1.setRepositoryNum(object.getInteger("repository_num"));


        int upda=yunjiayiMedicineMapper.updateByPrimaryKey(medicine1);
        int delmed=pictureMSMapper.deleteByMedicineId(medicine1.getMedicineId());
        PictureMS pictureMS=new PictureMS();
        for (Object file:object.getJSONArray("picture")){
            pictureMS.setPictureurl(file.toString());
            pictureMS.setMedicineid(medicine1.getMedicineId());
            int uppic=pictureMSMapper.insert(pictureMS);
        }

        if (upda==1){
            result.setCode(0);
            result.setMessage("修改成功");
        }else {
            result.setCode(1);
            result.setMessage("修改失败");
        }
        return result;
    }
}
